package com.inossem.warehousestorage_inossem.activity.allocation.in_storage;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.allocation.in_storage.extra.AllocationInStorageExtra;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.adapter.allocation.AllocationInPackageAdapter;
import com.inossem.warehousestorage_inossem.adapter.allocation.AllocationInSnAdapter;
import com.inossem.warehousestorage_inossem.bean.allocation.AllocationGetSNsByPackageBean;
import com.inossem.warehousestorage_inossem.bean.allocation.AllocationInMatsBean;
import com.inossem.warehousestorage_inossem.dialog.DialogUtils;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.allocation.AllocationGetSNsByPackageRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.AllocationUtils;
import com.inossem.warehousestorage_inossem.utils.BaseInteface;
import com.inossem.warehousestorage_inossem.utils.NoScrollListView;

import java.util.List;

/**
 * 默认仓位
 *
 * @author 詹建宇
 * @time on 2018/4/4 16:05
 * @email jianyu.zhan@inossem.com
 */
public class AllocationInDefaultSNsActivity extends BaseActivity implements BaseInteface {

    private TextView title;
    private NoScrollListView packageListView, snListView;

    private String mat_id;

    //物料
    private AllocationInMatsBean.MatsBodyBean.ItemListBean itemListBean;
    //该物料下的 默认仓位下的 序列号列表
    private List<AllocationInMatsBean.MatsBodyBean.ItemListBean.SN> list;
    //默认仓位下的 序列号列表中的 不重复的 打包码集合
    private List<String> codes;
    //默认仓位下的 序列号列表中的 不在打包码中的 序列号集合
    private List<String> snList;

    private AllocationInPackageAdapter packageAdapter;
    private AllocationInSnAdapter snAdapter;

    private EditText conditionEdit;
    private ImageView searchImg;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        super.onCreateImpl(savedInstanceState);
        setContentView(R.layout.cy_activity_in_storage_allocation_sn_listview);

        initView(this);
    }

    @Override
    protected void onResumeImpl() {
        super.onResumeImpl();
        initData(this);
        initOper(this);
    }

    private void getExtra(Activity activity) {
        mat_id = activity.getIntent().getStringExtra(AllocationInStorageExtra.MAT_ID);
    }

    @Override
    public void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        packageListView = (NoScrollListView) activity.findViewById(R.id.packageListView);
        snListView = (NoScrollListView) activity.findViewById(R.id.snListView);

        conditionEdit = (EditText) activity.findViewById(R.id.conditionEdit);
        searchImg = (ImageView) activity.findViewById(R.id.searchImg);
    }

    @Override
    public void initData(Activity activity) {
        getExtra(activity);
        title.setText(AllocationUtils.NO_CODE_POSITION);
        ActivityUtils.initBackAndFunction(activity, R.string.activity_receive_result_back, new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });
        itemListBean = AllocationUtils.getItemListBeanByMatId(activity, mat_id);
        list = itemListBean.getSn_List();
        codes = AllocationUtils.getDefaultPackageCodes(list);
        snList = AllocationUtils.getDefaultSNList(list);

        addPackageListData();
        addSNListData();
    }

    private void addPackageListData() {
        packageAdapter = new AllocationInPackageAdapter(this, codes);
        packageListView.setAdapter(packageAdapter);
    }

    private void addSNListData() {
        snAdapter = new AllocationInSnAdapter(this, snList);
        snListView.setAdapter(snAdapter);
    }

    private void initOper(Activity activity) {

        searchImg.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                searchSN(activity, conditionEdit.getText().toString().trim());
            }
        });

        packageListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                //进入序列号列表页

            }
        });

        packageListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                //删除
                DialogUtils.getConfirmDialog(activity, "确认删除?", new DialogUtils.ConfirmDialogListener() {
                    @Override
                    public void onConfirm(Dialog dialog) {
                        AllocationUtils.deleteDefaultPackage(activity, mat_id, codes.get(i));
                        codes.remove(i);
                        packageAdapter.notifyDataSetChanged();
                        dialog.dismiss();
                    }
                }).show();
                return true;
            }
        });

        snListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                //删除
                DialogUtils.getConfirmDialog(activity, "确认删除?", new DialogUtils.ConfirmDialogListener() {
                    @Override
                    public void onConfirm(Dialog dialog) {
                        AllocationUtils.deleteDefaultSN(activity, mat_id, snList.get(i));
                        snList.remove(i);
                        snAdapter.notifyDataSetChanged();
                        dialog.dismiss();
                    }
                }).show();
                return true;
            }
        });
    }

    private void searchSN(Activity activity, String condition) {
        AllocationGetSNsByPackageRequest.request(activity, condition, itemListBean.getLocation_id(), new GsonBeanListener<AllocationGetSNsByPackageBean>() {
            @Override
            public void onSuccess(AllocationGetSNsByPackageBean bean) {
                AllocationUtils.setSNsInDefaultPosition(activity, mat_id, bean);
                initData(activity);
            }
        });
    }

}
